import natural from "natural";
import { getData } from "./getData.js";
import { sampleSize } from "lodash-es";
const list = await getData();
/** 训练集与测试集的比例 */
const rate = 50;
const test = list.map((i) => {
    return {
        ...i,
        files: sampleSize(i.files, rate),
    };
});
var classifier: natural.BayesClassifier = await new Promise((resolve) => {
    natural.BayesClassifier.load(
        "./src/result/classifier.json",
        null as any,
        function (err, classifier) {
            if (err) {
                throw err;
            } else {
                resolve(classifier);
            }
        }
    );
});
console.time("init");
classifier.train();
console.timeEnd("init");
const result = {} as any;
console.log("测试集数目");
test.forEach((i) => {
    console.log(i.type, i.files.length);
    result[i.type] = { count: [0, 0], error: {} };
    i.files.forEach((content) => {
        const ans = classifier.classify(content);
        let index = i.type === ans ? 1 : 0;

        result[i.type].count[index]++;
        if (index === 0) {
            const record = result[i.type].error;
            if (ans in record) {
                record[ans]++;
            } else {
                record[ans] = 1;
            }
        }
    });
});
console.log(result);
